<?php
/*
UploadiFive
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
*/
if (!empty($_FILES)) {
    // Set the uplaod directory
    $suggest_folder = '/V6.1_2019070701';
    $uploadDir = '/Resource/DocumentFile/';
    $sn = $_GET['sn'];
    $state = $_GET['state'];
// Set the allowed file extensions
    $fileTypes = array('avi', 'mp3', 'mp4', 'bmp', 'ico', 'gif', 'jpeg', 'jpg', 'png', 'psd', 'rar', 'zip', 'swf', 'log', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'txt', 'xls', 'xlsx'); // Allowed file extensions

    $userId = $_COOKIE['user_id'];
    include '../db/sql.php';
    $from = $db->query("SELECT UserId,RealName from Base_User WHERE Account = '$userId'");
    $row = $from->fetch();
    $uName = $row['RealName'];
    $file_dir_1 = $row['UserId'];
    $file_dir_2 = date('Ymd');
    $uploadDir_sql = $uploadDir . $file_dir_1 . "/" . $file_dir_2 . "/";
    $uploadDir = $suggest_folder . $uploadDir . $file_dir_1 . "/" . $file_dir_2 . "/";
    if (!file_exists($uploadDir)) {
        if (!mkdir($uploadDir, 0777, true)) {
            die('baibaila');
        }
    }

    $tempFile = $_FILES['Filedata']['tmp_name'];
    //$uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
    $targetFile_sql = $uploadDir_sql . GUIDv4() . "." . pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
    $targetFile = $suggest_folder . $targetFile_sql;
    // Validate the filetype
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        // Save the file
        save_file();
        $Fname = pathinfo($_FILES['Filedata']['name'], PATHINFO_BASENAME);
        $targetFile = "~" . $targetFile_sql;
        $size = $_FILES['Filedata']['size'];
        $type = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
        $extesion = "." . $type;
        $date_now = udate("Y-m-d H:i:s.u");
        $from = $db->query("SELECT NEWID()");
        $row = $from->fetch();
        $db->exec("INSERT INTO Base_FileInfo(FileId,FolderId,FileName,FilePath,FileSize,FileExtensions,FileType,DeleteMark,EnabledMark,CreateDate,CreateUserId,CreateUserName,ModifyDate,ModifyUserId,ModifyUserName) VALUES ('$row[0]',0,'$Fname','$targetFile','$size','$extesion','$type',0,1,'$date_now','$file_dir_1','$uName','$date_now','$file_dir_1','$uName')");
        $db->exec("INSERT INTO T_SuggestFile(ID,SN,AddTime,FileId,Cate) VALUES (NEWID(),'$sn','$date_now','$row[0]','$state')");
    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}


function GUIDv4($trim = true)
{
    // Windows
    if (function_exists('com_create_guid') === true) {
        if ($trim === true)
            return trim(com_create_guid(), '{}');
        else
            return com_create_guid();
    }

    // Fallback (PHP 4.2+)
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $lbrace = $trim ? "" : chr(123);    // "{"
    $rbrace = $trim ? "" : chr(125);    // "}"
    $guidv4 = $lbrace .
        substr($charid, 0, 8) . $hyphen .
        substr($charid, 8, 4) . $hyphen .
        substr($charid, 12, 4) . $hyphen .
        substr($charid, 16, 4) . $hyphen .
        substr($charid, 20, 12) .
        $rbrace;
    return $guidv4;
}

function save_file()
{
    global $targetFile;
    global $uploadDir;
    if (file_exists($targetFile)) {
        $targetFile = $uploadDir . GUIDv4() . "." . pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
        save_file();
    } else {
        global $tempFile;
        move_uploaded_file($tempFile, $targetFile);
        echo 1;
    }
}

function udate($format = 'u', $utimestamp = null)
{
    if (is_null($utimestamp)) {
        $utimestamp = microtime(true);
    }
    $timestamp = floor($utimestamp);
    $milliseconds = round(($utimestamp - $timestamp) * 1000);//改这里的数值控制毫秒位数
    return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}